//
//  CJChannelController.m
//  Boss
//
//  Created by admin on 2018/9/12.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "CJChannelController.h"
#import "SGPagingView.h"
#import "CJChannelSubController.h"
#import "CJFourBusiness.h"
@interface CJChannelController ()<SGPageTitleViewDelegate,SGPageContentScrollViewDelegate>
@property (nonatomic, strong) SGPageTitleView *pageTitleView;
@property (nonatomic, strong) SGPageContentScrollView *pageContentView;
@property (nonatomic,strong) NSMutableArray *channelArray;
@end

@implementation CJChannelController
-(NSMutableArray *)channelArray
{
    if (!_channelArray) {
        _channelArray = [[NSMutableArray alloc]init];
    }
    return _channelArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"渠道销售";
    // [self setupUI];
    [self loadData];
    // Do any additional setup after loading the view.
}

-(void)loadData
{
    WEAKSELF
    [WYHttpServer getBusinessTypeWithSortId:@(self.sortId) SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        
        if (success) {
            [weakSelf.channelArray addObjectsFromArray:[CJFourBusiness getFourBusinessModelFromJson:response[@"result"]]];
            [weakSelf setupUI];
        }
    }];
}


-(void)setupUI
{
    // NSArray *titleArr = @[@"我的关注", @"我的粉丝"];
    NSMutableArray *titleArr = [[NSMutableArray alloc]init];
    NSMutableArray *childVCArr = [[NSMutableArray alloc]init];
    for (CJFourBusiness *business in self.channelArray) {
        [titleArr addObject:business.name];
        CJChannelSubController *oneVC = [[CJChannelSubController alloc]init];
        oneVC.sortId = business.sortId;
//        int R = (arc4random() % 256) ;
//        int G = (arc4random() % 256) ;
//        int B = (arc4random() % 256) ;
//
//        oneVC.view.backgroundColor = RGBA(R, G, B, 1.0);
        
        
        [childVCArr addObject:oneVC];
    }
    
    
    
    SGPageTitleViewConfigure *configure = [SGPageTitleViewConfigure pageTitleViewConfigure];
    configure.titleColor =[UIColor hexStringToColor:@"#666666" alpha:1.0];
    configure.titleSelectedColor = [UIColor hexStringToColor:@"#d8593f" alpha:1.0];
    configure.indicatorColor = [UIColor whiteColor];
    configure.indicatorHeight = 2.f;
    configure.indicatorStyle = SGIndicatorStyleDefault;
    configure.showBottomSeparator = NO;
    configure.indicatorAdditionalWidth = 4;
    configure.indicatorFixedWidth = 4.f;
    configure.indicatorColor = kColorMainColor;
    
    self.pageTitleView = [SGPageTitleView pageTitleViewWithFrame:CGRectMake(0, self.navHeight, self.view.frame.size.width, 43) delegate:self titleNames:titleArr configure:configure];
    // [self.view addSubview:_pageTitleView];
    self.pageTitleView.backgroundColor = [UIColor whiteColor];
    //    self.pageTitleView.titleColorStateNormal = [UIColor hexStringToColor:@"#f7ddd8" alpha:1.0];
    //    self.pageTitleView.titleColorStateSelected = [UIColor hexStringToColor:@"#ffffff" alpha:1.0];
    //    self.pageTitleView.indicatorColor = [UIColor whiteColor];
    //    self.pageTitleView.indicatorHeight = 4.f;
    //    self.pageTitleView.indicatorLengthStyle = SGIndicatorLengthTypeDefault;
    //    self.pageTitleView.isShowBottomSeparator = NO;
    [self.view addSubview: self.pageTitleView];
    
    
    //    for
    
    
    CGFloat contentViewHeight = kScreenHeight - self.navHeight-43;
    self.pageContentView = [[SGPageContentScrollView alloc] initWithFrame:CGRectMake(0, self.navHeight+43, self.view.frame.size.width, contentViewHeight) parentVC:self childVCs:childVCArr];
    _pageContentView.delegatePageContentScrollView = self;
    [self.view addSubview:_pageContentView];
}


- (void)pageContentScrollView:(SGPageContentScrollView *)pageContentScrollView progress:(CGFloat)progress originalIndex:(NSInteger)originalIndex targetIndex:(NSInteger)targetIndex
{
    NSLog(@"-------%ld",targetIndex);
    //    [self.twoVc refreshloadData];
    //    [self.oneVc refreshloadData];
    [self.pageTitleView setPageTitleViewWithProgress:progress originalIndex:originalIndex targetIndex:targetIndex];
}
- (void)pageTitleView:(SGPageTitleView *)pageTitleView selectedIndex:(NSInteger)selectedIndex
{
    //    [self.twoVc refreshloadData];
    //    [self.oneVc refreshloadData];
    NSLog(@"------------------%ld",selectedIndex);
    [self.pageContentView setPageContentScrollViewCurrentIndex:selectedIndex];
}

@end
